<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>左树右表</title>
    <link rel="stylesheet" href="../../../assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../../assets/module/dtree/dtree.css"/>
    <link rel="stylesheet" href="../../../assets/module/dtree/font/dtreefont.css"/>
    <link rel="stylesheet" href="../../../assets/module/admin.css?v=311"/>
    <style>
        #ltTree {
            height: 535px;
            overflow: auto;
        }

        @media screen and (max-width: 750px) {
            #ltTree {
                height: auto;
            }
        }
    </style>
</head>

<body>

<!-- 加载动画，移除位置在common.js中 -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <!-- 左树 -->
        <div class="layui-col-sm12 layui-col-md4 layui-col-lg3">
            <div class="layui-card">
                <div class="layui-card-body mini-bar" id="ltTree">

                </div>
            </div>
        </div>
        <!-- 右表 -->
        <div class="layui-col-sm12 layui-col-md8 layui-col-lg9">
            <div class="layui-card">
                <div class="layui-card-body" style="min-height: 535px;">

                    <div class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label w-auto">搜索：</label>
                                <div class="layui-input-inline mr0">
                                    <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn icon-btn" id="btnSearch">
                                    <i class="layui-icon">&#xe615;</i>搜索
                                </button>
                                <button id="btnAdd" class="layui-btn icon-btn">
                                    <i class="layui-icon">&#xe654;</i>新增
                                </button>
                                <button id="btnDel" class="layui-btn layui-btn-danger icon-btn">
                                    <i class="layui-icon">&#xe640;</i>删除
                                </button>
                            </div>
                        </div>
                    </div>

                    <table class="layui-table" id="rtTable" lay-filter="rtTable"></table>

                </div>
            </div>
        </div>

    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="user-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- js部分 -->
<script type="text/javascript" src="../../../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../../../assets/js/common.js?v=311"></script>

<script>
    layui.use(['layer', 'form', 'table', 'util', 'dtree'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var dtree = layui.dtree

        // 渲染表格
        var ins1 = table.render({
            elem: '#rtTable',
            url: '../../../json/user.json',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {field: 'username', align: 'center', sort: true, title: '账号'},
                {field: 'nickName', align: 'center', sort: true, title: '用户名'},
                {field: 'phone', align: 'center', sort: true, title: '手机号'},
                {field: 'sex', align: 'center', sort: true, title: '性别'},
                {
                    sort: true, align: 'center', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', align: 'center', toolbar: '#user-table-bar', title: '操作', minWidth: 120}
            ]]
        });

        // 监听工具条
        table.on('tool(rtTable)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值

            if (layEvent === 'edit') { // 查看
                layer.msg('点击了修改');
            } else if (layEvent === 'del') { // 删除
                layer.msg('点击了删除');
            }
        });

        // 批量删除
        $('#btnDel').click(function () {
            var checkRows = table.checkStatus('rtTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要删除的数据', {icon: 2});
            } else {
                layer.msg('你选择了' + checkRows.data.length + '条数据', {icon: 1});
            }
        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var value = $('#edtSearch').val();
            table.reload('rtTable', {where: {search: value}});
        });

        // 树形渲染
        dtree.render({
            elem: '#ltTree',
            url: '../../../json/tree.json',
            type: 'all',
            initLevel: '2',
            dot: false,
            method: 'GET'
        });
        // 树形点击事件
        dtree.on('node("ltTree")', function (obj) {
            var data = obj.param;
            layer.msg('你选择了：' + data.nodeId + '-' + data.context);
            table.reload('rtTable', {where: {nodeId: data.nodeId}});
        });

    });
</script>
</body>

</html>